<?php

namespace backend\models;

use Yii;
use yii\behaviors\TimestampBehavior;

/**
 * This is the model class for table "{{%jjr_project}}".
 *
 * @property string $id
 * @property string $name 楼盘名称
 * @property int $price 售价(单位：元/平方)
 * @property string $main_pic 楼盘主图
 * @property string $commission_ad 佣金广告词
 * @property int $commission_percent 佣金百分比
 * @property double $commission_fix 固定金额
 * @property string $commission_type 佣金计算方式
 * @property string $adviser 置业顾问
 * @property string $adviser_mobile 置业顾问电话
 * @property string $description 楼盘描述
 * @property string $address 楼盘地址
 * @property string $longitude 经度
 * @property string $latitude 纬度
 * @property string $create_time 创建时间
 * @property string $update_time 更新疏忽
 * @property int $status 状态
 * @property int $province 省
 * @property int $city 市
 * @property int $area 区
 * @property string $tel 现场热线
 * @property string $price_name 价格名称
 * @property string $refer_start_time 转介开始时间
 * @property string $refer_end_time 转介结束时间
 *@property string $real_pic 实景图
 * @property string $vr_pic 效果图
 * @property string $around_pic 周边图
 */
class JjrProject extends \common\core\BaseActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%jjr_project}}';
    }
    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    # 创建之前
                    \yii\db\ActiveRecord::EVENT_BEFORE_INSERT => ['create_time', 'update_time'],
                    # 修改之前
                    \yii\db\ActiveRecord::EVENT_BEFORE_UPDATE => ['update_time']
                ],
                #设置默认值
                'value' =>date('Y-m-d H:i:s')
            ]
        ];
    }


    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name', 'main_pic', 'commission_ad', 'commission_type', 'adviser', 'adviser_mobile','address','price_name'], 'required'],
            ['adviser_mobile','match','pattern'=>'/^[1][34578][0-9]{9}$/'],
            [['refer_start_time','refer_end_time'],'date','format' => 'yyyy-mm-dd'],
            ['tel','match','pattern'=>'/^0\d{2,3}(-)?\d{7,8}$/'],
            [['price', 'commission_percent', 'status', 'province', 'city', 'area'], 'integer'],
            [['commission_fix', 'longitude', 'latitude'], 'number'],
            [['create_time', 'update_time'], 'safe'], //'commission_percent', 'commission_fix'
            ['commission_percent', 'compare', 'compareValue' => 0, 'operator' => '>'],
            ['commission_percent', 'compare', 'compareValue' => 100, 'operator' => '<'],
            [['name', 'main_pic', 'commission_ad', 'commission_type', 'adviser', 'adviser_mobile', 'description', 'address','tel'], 'string', 'max' => 255],
            ['status','default','value'=>0] //默认值为未审核
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => '楼盘名称',
            'price_name'=>'价格名称',
            'price' => '售价(单位：元/平方)',
            'main_pic' => '楼盘主图',
            'commission_ad' => '佣金广告词',
            'commission_percent' => '佣金百分比',
            'commission_fix' => '固定金额',
            'commission_type' => '佣金计算方式',
            'adviser' => '销售经理',
            'adviser_mobile' => '销售经理电话',
            'description' => '楼盘描述',
            'address' => '楼盘地址',
            'longitude' => '经度',
            'latitude' => '纬度',
            'create_time' => '创建时间',
            'update_time' => '更新时间',
            'status' => '状态',
            'tel' => '现场热线',
            'refer_start_time' => '转介开始时间',
            'refer_end_time' => '转介结束时间',
        ];
    }

    public function getMainPicPath(){
        $pic = Picture::findOne($project['main_pic']);
        $project['main_pic_path'] = Url::to('@storageUrl/image/'.$pic['path'],true);
    }
}
